package com.czh.subject.infra.utils;

import com.alibaba.druid.filter.config.ConfigTools;
import lombok.extern.slf4j.Slf4j;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * 数据库加密util
 *
 * @author : caozaihui
 * @since : 2024/8/21
 */
@Slf4j
public class DruidEncryptUtil {

    private static String publicKey;

    private static String privateKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println("privateKey:" + privateKey);
            publicKey = keyPair[1];
            System.out.println("publicKey:" + publicKey);
        } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
            log.error("数据库加密失败:{}",e.getMessage(),e);
        }
    }

    public static String encrypt(String plainText) throws Exception {
        return ConfigTools.encrypt(privateKey, plainText);
    }

    public static String decrypt(String encryptText) throws Exception {
        return ConfigTools.decrypt(publicKey, encryptText);
    }

//    public static void main(String[] args) throws Exception {
////        String encrypt = encrypt("123456");
////        System.out.println("encrypt:" + encrypt);
//        System.out.println("decrypt:"+decrypt("CvLe+z+dNeBnhlaAUtNqrZmnBJClYcWxrCt3K9IKi5r12DXimYfF5CJ9CKJxjNdzQePV6vubi8g0403iy5FquQ=="));
//    }

}
